package com.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.entity.Staff;
import com.project.service.StaffService;
import com.project.util.PageBean;
import com.project.util.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 员工控制器
 *
 * @author Leo高洋
 * @create 2023-01-17 0:31
 */
@RestController
@RequestMapping("/staff")
public class StaffController {
    @Autowired
    private StaffService staffService;

    private final static Logger logger = LoggerFactory.getLogger(StaffController.class);

    /**
     * 根据条件分页查询员工信息
     *
     * @param pageBean
     * @return
     */
    @PostMapping("/list")
    public R staffList(@RequestBody PageBean pageBean) {
        logger.info("根据条件分页查询员工信息");
        Map<String, Object> map = new HashMap<>();
        map.put("name", pageBean.getQuery().trim());
        map.put("mec_id", pageBean.getQuery2().trim());
        map.put("start", pageBean.getStart());
        map.put("pageSize", pageBean.getPageSize());
        List<Staff> staffList = staffService.list(map);
        long total = staffService.getTotal(map);
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("staffList", staffList);
        resultMap.put("total", total);
        return R.ok(resultMap);
    }

    /**
     * 查询所有员工姓名
     *
     * @return
     */
    @GetMapping("/listOffice")
    public R staffOfficeList() {
        logger.info("查询所有员工姓名");
        List<Staff> staffOfficeList = staffService.getList();
        Map<String, Object> map = new HashMap<>();
        map.put("staffOfficeList", staffOfficeList);
        return R.ok(map);
    }

    /**
     * 根据部门id查询员工信息
     *
     * @param id
     * @return
     */
    @GetMapping("/list/{id}")
    public R getListById(@PathVariable(value = "id") Integer id) {
        logger.info("根据部门id查询员工信息");
        List<Staff> staffList = staffService.list(new QueryWrapper<Staff>().eq("mec_id", id));
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("staffList", staffList);
        return R.ok(resultMap);
    }
}
